const App = getApp();
Page({
    data: {
        key: 0,
        isLoading: true,
        reLoading: false,
    },
    onLoad: function (options) {
        let _this = this;
        _this.setData({
            key: options.key
        })
        _this.getReplaceDetail();
    },

    // 获取替换详情
    getReplaceDetail() {
        let _this = this;
        App._get('Generate/detail', {
            key: _this.data.key
        }, res => {
            _this.setData(res.data)
        }, false, () => {
            _this.setData({
                isLoading: false,
                reLoading: false,
            })
        });
    },

    // 下载二维码
    onSaveQrcode() {
        let _this = this;
        _this.setData({
            reLoading: true
        })
        // 下载图片二维码
        wx.downloadFile({
            url: _this.data.detail.replace_url,
            success(res) {
                wx.hideLoading();
                // 图片保存到本地
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success(data) {
                        wx.showToast({
                            title: '保存成功',
                            icon: 'success',
                            duration: 2000
                        });
                    },
                    fail(err) {
                        console.log(err.errMsg);
                        if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
                            wx.showToast({
                                title: "请允许访问相册后重试",
                                icon: "none",
                                duration: 1000
                            });
                            setTimeout(() => {
                                wx.openSetting();
                            }, 1000);
                        }
                    },
                    complete(res) {
                        console.log('complete');
                        // wx.hideLoading();
                        _this.setData({
                            reLoading: false
                        })
                    }
                })
            }
        })
    },


});
